package org.xin.generate.controller;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.xin.common.result.Result;
import org.xin.framework.base.controller.IBaseController;
import org.xin.generate.pojo.dto.GenerateDTO;
import org.xin.generate.pojo.entity.Generate;
import org.xin.generate.service.GenerateService;


/**
 * @author ：xin
 * @date ：Created in 9:31
 * @description：
 * @modified By：$
 * @version: $
 */
@Api(tags = "代码生成")
@CrossOrigin
@RestController
@RequestMapping("/xin/generate")
public class GenerateController extends IBaseController<Generate, GenerateService> {

    @ApiOperation("生成代码")
    @GetMapping("/made/{id}")
    public Result made(@PathVariable("id") String id){
        Generate generate = service.getById(id);
        AutoGenerator generator = service.init(generate);
        return Result.check(service.generator(generator));
    }

    @ApiOperation("获取当前数据库表集合")
    @PostMapping("/tables")
    public Result tables(){
        return Result.success(service.getSchemas());
    }


    @ApiOperation("添加/编辑配置")
    @PostMapping("/edit")
    public Result edit(GenerateDTO dto){
        Generate generate = new Generate();
        BeanUtils.copyProperties(dto, generate);
        return Result.check(service.saveOrUpdate(generate));
    }

    @ApiOperation("获取源配置列表")
    @GetMapping({"list", "list/{limit}", "list/{page}/{limit}"})
    public Result list(
            @PathVariable(value = "page", required = false)
            @ApiParam(value = "页", example = "1", defaultValue = "1", type = "int") Integer page,
            @PathVariable(value = "limit", required = false)
            @ApiParam(value = "条", example = "10", defaultValue = "10", type = "int") Integer limit
    ){
        return Result.success(service.page(page, limit));
    }
}
